package com.railway.switchs.action.system;


import com.railway.switchs.domain.response.EnumResponse;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.utils.EnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName EnumHandleAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/enum")
public class EnumHandleAction {

    /**
     * 前台获取枚举类的全部枚举
     * @param enumClassName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllEnums",method = RequestMethod.POST)
    public EnumResponse getAllEnums(String enumClassName){
        EnumResponse response = new EnumResponse();
        try{
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setData(EnumUtil.getEnumList(enumClassName));
        }catch (Exception e){
            log.error("获取枚举类{}数据异常:{}",enumClassName,e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("系统异常");
        }
        return response;
    }

    /**
     *  通过code获取枚举类的name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getEnumValue",method = RequestMethod.POST)
    public EnumResponse getEnumValue(String enumClassName, String code){
        EnumResponse response = new EnumResponse();
        try{
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setName(EnumUtil.getEnumValue(enumClassName,code));
        }catch (Exception e){
            log.error("获取枚举类{}Code{}异常:{}",enumClassName,code,e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("系统异常");
        }
        return response;
    }
}
